<template>
  <div>
    <base-list ref="warehouse"
               :condition="condition"
               :datas.sync="warehouse"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="7" class="search-width4">
            <el-form-item label="仓库代码">
              <auto-search-warehouse-code @select="v=>condition['search_LIKE_code']=v"></auto-search-warehouse-code>
            </el-form-item>
          </el-col>
          <el-col :span="7" class="search-width4">
            <el-form-item label="仓库名称">
              <auto-search-warehouse-name @selectName="v=>condition['search_LIKE_name']=v"></auto-search-warehouse-name>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="search-width4">
            <el-form-item label="所属单位"
                          prop="orgId">
              <auto-search-org v-model="form.orgId"
                               @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                               @selectName="v=>form.orgName=v"
                               :disabled="true"
                               :name="form.orgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ml-80"
                       @click="loadData">查 询</el-button>
            <!-- <el-button formVisible="true" icon="el-icon-plus" @click="handleDialogFormOpen(false)" v-if="power('WJOB_BASEMGR_VEHICLE:ADD')">
                            新增
                        </el-button> -->
          </el-col>
        </el-row>
      </el-form>
      <el-table slot="list"
                :data="warehouse"
                border
                stripe
                max-height="550">
        <el-table-column type="index"
                         fixed="left"
                         label="序号"
                         width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column prop="code"
                         label="仓库代码"
                         min-width="130"></el-table-column>
        <el-table-column prop="name"
                         label="仓库名称"
                         min-width="130"></el-table-column>
        <!-- <el-table-column prop="categoryName"
                         label="仓库类型"
                         min-width="120"></el-table-column> -->
        <!-- <el-table-column prop="teu"
                         label="联系人"
                         
                         min-width="120"></el-table-column> -->
        <!-- <el-table-column prop="weight"
                         label="联系电话"
                         
                         min-width="120"></el-table-column> -->
        <!-- <el-table-column prop="terminalName" label="对接终端"  min-width="120"></el-table-column> -->
        <el-table-column prop="orgName"
                         label="所属单位"
                         min-width="200"></el-table-column>
        <el-table-column prop="location"
                         label="所在地"
                         min-width="200"></el-table-column>
        <el-table-column prop="remark"
                         label="备注"
                         min-width="120">
        </el-table-column>
        <!-- <el-table-column prop="resourceDescribe" label="备注"  min-width="200"></el-table-column> -->
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
                         width="140">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       @click="handleDialogFormOpen(true,scope.row)">
              编辑
            </el-button>
            <el-button type="text"
                       class="danger"
                       size="small"
                       @click="handleRemove(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <admin-warehouse-add-form ref="adminWarehouseAddForm"
                              @load="loadData"></admin-warehouse-add-form>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 plain
                 @click="handleRouterPush({path: '/goods/storehouse',name: '仓库管理'})">返回</el-button>
    </div>
  </div>
</template>

<script>
import {
  getWarehouseList,
  deleteWarehouse
} from '@/api/adminWarehouse/adminWarehouse'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchVehicleNo from '@/components/autocomplete/autoSearchVehicleNo'
import autoSearchWarehouseCode from '@/components/autocomplete/autoSearchWarehouseCode'
import autoSearchWarehouseName from '@/components/autocomplete/autoSearchWarehouseName'
import adminWarehouseAddForm from './adminWarehouseAddForm'

export default {
  name: 'adminWarehouse',
  components: {
    autoSearchOrg,
    autoSearchVehicleNo,
    autoSearchDic,
    autoSearchWarehouseCode,
    autoSearchWarehouseName,
    adminWarehouseAddForm
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, // 查询条件
      warehouse: [], // table数组
      pageSearch: getWarehouseList, // 分页查询api接口
      id: ''
    }
  },
  mounted() {
    getWarehouseList().then(res => {
      console.log(12,res.data.aaData)
    })
    this.warehouseList()
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.warehouse.loadData()
    },
    handleDialogFormOpen(bool, item) {
      if (bool) {
        let form = JSON.parse(JSON.stringify(item))
        this.$refs.adminWarehouseAddForm.form = {
          id: form.id,
          orgId: form.orgId,
          orgName: form.orgName,
          code: form.code,
          name: form.name,
          location: form.location,
          remark: form.remark
        }
        this.$refs.adminWarehouseAddForm.isEdit = true
        this.$refs.adminWarehouseAddForm.dialogForm = true
      } else {
        this.$refs.adminWarehouseAddForm.dialogForm = true
      }
    },
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteWarehouse(item.id).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 获取所属仓库
    warehouseList() {
      getWarehouseList().then(res => {
        let dataList = res.data.aaData
        let warehouseList = []
        for (let i = 0; i < dataList.length; i++) {
          warehouseList.push(dataList[i].name)
        }
        console.log(warehouseList)
      })
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
